/**
 * Class to create a simple FTP client. 
 * <p>
 * Copyright 2013 / All Rights Reserved
 * <p>
 * EDAG Production Solutions GmbH & Co. KG <br>
 * Leit- und Steuerungstechnik <br>
 * Reesbergstrasse 1           <br>
 * D - 36039 Fulda             <br>
 * <p>
 * @file:   FTPclient.java
 * @author:  rf20758
 * @version: 0.1
 * @date:   10.11.2013
 *
 * History: [WHO WHEN WHAT has changed / adapted]
 * New entries always come before the old entries.
 */

package com.efast.dmom.mes.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.util.ConfigUtil;




public class FTPclient 
{	
	
	FTPClient client = new FTPClient();
	FTPClientConfig config = new FTPClientConfig();
	FileInputStream fis = null;
	
	String host;
	String user;
	String pass;
	String stat;
	String path;
	String filename;
	String printer;
	String processName;
	
	public FTPclient()
	{
	}
	
	public void sendLayoutToPrinter(String identOfManifest) 
	{
		//Load the connection parameters from the configuration file
//		if(PrinterPropertiesHepler.getValue(identOfManifest+"_PTR") != null)
//		{
//			printer = PrinterPropertiesHepler.getValue(identOfManifest+"_PTR");
			host = ConfigUtil.getValue("ftp.print.host");
			user = ConfigUtil.getValue("ftp.print.user");
			pass = ConfigUtil.getValue("ftp.print.pass");
			stat = ConfigUtil.getValue("ftp.print.stat");
			path = ConfigUtil.getValue("ftp.print.path");
//		}
//		//If the identOfManifest is unknown
//		else
//		{
//			host = PrinterPropertiesHepler.getValue("DEFAULT_HOST");
//			user = PrinterPropertiesHepler.getValue("DEFAULT_USER");
//			pass = PrinterPropertiesHepler.getValue("DEFAULT_PASS");	
//			stat = PrinterPropertiesHepler.getValue("DEFAULT_STAT");
//			path = PrinterPropertiesHepler.getValue("DEFAULT_OUTPUT_PATH");
//		}
		path = ConfigUtil.getUploadDir();
		filename = path+"print.ps";
		try 
		{	
			//Connect to the printer server when the status of the printer is enabled.
			if(stat.equals("enable"))
			{
				client.connect(host);
				client.login(user, pass);
				fis = new FileInputStream(filename);				
				client.storeFile("print.ps", fis);														
				fis.close();
				client.logout();
			}
			else
			{
				
				   			
			}
			
		} 
		catch (IOException e) 
		{
			
//			try{
//				sendemail("Process "+processName+": FTPClient: Error: The printer "+host +" "+ e.getMessage());
//			}
//			catch (Exception ex) {
//				
//				     logger.error (new LogObjekt ("","","Exception on sending error email "+ ex.getMessage()));
//				     throw new ServiceException("Exception on sending error email "+ ex.getMessage());
//				     
//			}
			 throw new ServiceException("FTPClient: Error: The printer  "+host+" "+ e.getMessage());
			
		}
		
	}

//	public static void sendemail(String emailContent) 
//	{
//		try{
//			SimpleMailSender sms = new SimpleMailSender("email.qorosauto.com","MESOnline@qorosauto.com","Qoros111");										
//			ArrayList<String>  recipents = new ArrayList<String>();
//			//recipents.add("pps.core@qorosauto.com");
//			//recipents.add("pps.support@qorosauto.com");
//			recipents.add("terry.shi@qorosauto.com");
//			recipents.add("rock.cai@qorosauto.com");
//			recipents.add("rong.meng@qorosauto.com");
//			recipents.add("maverick.chen@qorosauto.com");
//			recipents.add("levis.zhang@qorosauto.com");
//			recipents.add("gerhard.claus@qorosauto.com");			
//			recipents.add("bobby.song-v@qorosauto.com");
//			recipents.add("suturn.lu-v@qorosauto.com");
//			recipents.add("tino.xu-v@qorosauto.com");
//			recipents.add("zhen.chen-v@qorosauto.com");
//			recipents.add("winter.xu-v@qorosauto.com");
//			recipents.add("sky.lu-v@qorosauto.com");			
//			sms.send(recipents, "INTJIS Error", emailContent);						
//		}
//		catch (Exception ex) {
//		}
//	}
	
	public String getPrinterName() 
	{
		return host;
	}
}
